const { DataTypes } = require('sequelize')

module.exports = (sequelize) => {
  const Permission = sequelize.define('Permission', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: '权限名称'
    },
    code: {
      type: DataTypes.STRING(100),
      allowNull: false,
      unique: true,
      comment: '权限代码'
    },
    resource: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: '资源标识'
    },
    action: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: '操作类型'
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '权限描述'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  }, {
    tableName: 'permissions',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '权限表',
    indexes: [
      {
        unique: true,
        fields: ['code']
      },
      {
        fields: ['resource']
      },
      {
        fields: ['action']
      }
    ]
  })

  return Permission
}